let activeEffect = null;
const useState = (value) => {
  const fns = new Set();
  const getter = () => {
    if(activeEffect) {
      fns.add(activeEffect);
    }
    return value;
  }
  const setter = (newValue) => {
    value = newValue;
    for(const fn of [...fns]) {
      fn();
    }
  }
  return [getter, setter];
}

const useEffect = (fn) => {
  activeEffect = fn;
  try {
    fn();
  } finally {
    activeEffect = null;
  }
}

const useMemo = (callback) => {
  const [s, set] = useState();
  useEffect(() => set(callback()));
  return s;
}

const [name1, setName1] = useState('KaSong');
const [name2, setName2] = useState('XiaoMing');
const [showAll, triggerShowAll] = useState(true);

const whoIsHere = useMemo(() => {
  if (!showAll()) {
    return name1();
  }
  return `${name1()} 和 ${name2()}`;
})

useEffect(() => console.log('谁在那儿！', whoIsHere()))


// // 测试
// const [name1, setName1] = useState('1');
// const [name2, setName2] = useState('a');
// useEffect(() => {
//   console.log('name1: '+name1());
//   console.log('name2: '+name2());
// }) 
// setName1('666');
// setName2('aaaa');
